package fw.asmx.launcher;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import fw.asmx.launcher.ui.TileLayout;
import fw.asmx.launcher.ui.TileView;

import java.util.List;

public class TileActivity extends Activity
{
    private TileLayout tl;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tile);
        if (savedInstanceState == null)
        {
            tl = (TileLayout) findViewById(R.id.vf_applst_applst);
            tl.setOnTileClickListener(new TileLayout.OnTileClickListener()
            {
                @Override public void onTileClick(TileView v)
                {
                    startActivity(getPackageManager().getLaunchIntentForPackage(v.getAppPkgName()));
                    finish();
                }
            });

            tl.setOnTileLongClickListener(new TileLayout.OnTileLongClickListener()
            {
                @Override public boolean onTileLongClick(TileView v)
                {
                    Toast.makeText(getApplicationContext(), "LONG", Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
        }
    }

/**
    private void setUnReadMsgCount(int x)
    {
        String dpkg = getDftSmsPkgName();

        int cnt = al_ai.size();
        for (int i = 0, j = 0; i < cnt; i++)
        {
            View v = tl.getChildAt(j);
            GridView gv = (GridView) v.findViewById(R.id.gv_gv_lst);
            String spkg = ((AppListAdapter) gv.getAdapter()).getItem(i).getPkgName();
            if (spkg.equals(dpkg))
            {
                View target = findViewById(R.id.iv_applst_icon);
                BadgeView badge = new BadgeView(this, target);
                badge.setText(x + "");
                badge.show();
                break;
            }
            j = i == 20 ? ++j : j;
        }
    }*/

    private String getDftCallPkgName()
    {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        PackageManager pm = getPackageManager();
        List<ResolveInfo> lst = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        Toast.makeText(getApplicationContext(), lst.get(0).activityInfo.packageName,
                       Toast.LENGTH_LONG).show();
        return lst.get(0).activityInfo.packageName;
    }

    private String getDftSmsPkgName()
    {
        Intent intent;
        PackageManager pm = getPackageManager();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            return Settings.Secure.getString(getContentResolver(), "sms_default_application");
        }
        else
        {
            intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setType("vnd.android-dir/mms-sms");
            List<ResolveInfo> lst =
                    pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
            Toast.makeText(getApplicationContext(), lst.get(0).activityInfo.packageName,
                           Toast.LENGTH_LONG).show();
            return lst.get(0).activityInfo.packageName;
        }
    }

    public void onItemClick(AdapterView<?> parent, View vv, int position, long id)
    {
        //String pkg = ((AppListAdapter) parent.getAdapter()).getItem(position).getPkgName();
        // startActivity(getPackageManager().getLaunchIntentForPackage(pkg));
    }
}
